import {DvLayout, getDuration} from "./Enums";

const DvLayoutModule = {
    namespaced: true,
    state: {
        layoutStatus: DvLayout.LayoutStatus.DISPERSED,
        focusStatus: DvLayout.FocusStatus.FOCUSED_OUT,
        err: ""
    },
    mutations: {
        [DvLayout.Mutations.SET_LAYOUT_STATUS]: (state, payload) => state.layoutStatus = payload,
        [DvLayout.Mutations.SET_FOCUS_STATUS]: (state, payload) => state.focusStatus = payload,
        setErrMsg: (state, payload) => state.err = payload
    },
    actions: {
        [DvLayout.Actions.TILE]: ({state, commit}) => {
            if (state.layoutStatus !== DvLayout.LayoutStatus.DISPERSED) {
                let err = "layout cannot be tiled by status but not dispersed!"
                console.error("layout error", err)
                commit("setErrMsg", err)
                return
            }
            commit("setErrMsg", "")
            commit(DvLayout.Mutations.SET_LAYOUT_STATUS, DvLayout.LayoutStatus.TILING)
            setTimeout(() => {
                commit(DvLayout.Mutations.SET_LAYOUT_STATUS, DvLayout.LayoutStatus.TILED)
            }, getDuration())
        },
        [DvLayout.Actions.DISPERSE]: ({state, commit}) => {
            if (state.layoutStatus !== DvLayout.LayoutStatus.TILED) {
                let err = "layout cannot be dispersed by status but not tiled!"
                console.error("layout error", err)
                commit("setErrMsg", err)
                return
            }
            commit("setErrMsg", "")
            commit(DvLayout.Mutations.SET_LAYOUT_STATUS, DvLayout.LayoutStatus.DISPERSING)
            setTimeout(() => {
                commit(DvLayout.Mutations.SET_LAYOUT_STATUS, DvLayout.LayoutStatus.DISPERSED)
            }, getDuration())
        },
        [DvLayout.Actions.FOCUS]: ({state, commit}) => {
            if (state.focusStatus !== DvLayout.FocusStatus.FOCUSED_OUT) {
                let err = "layout cannot be focused by status but not focused-out!"
                console.error("layout error", err)
                commit("setErrMsg", err)
                return
            }
            commit("setErrMsg", "")
            commit(DvLayout.Mutations.SET_FOCUS_STATUS, DvLayout.FocusStatus.FOCUSING)
            setTimeout(() => {
                commit(DvLayout.Mutations.SET_FOCUS_STATUS, DvLayout.FocusStatus.FOCUSED)
            }, getDuration())
        },
        [DvLayout.Actions.FOCUS_OUT]: ({state, commit}) => {
            if (state.focusStatus !== DvLayout.FocusStatus.FOCUSED) {
                let err = "layout cannot be focused out by status but not focused!"
                console.error("layout error", err)
                commit("setErrMsg", err)
                return
            }
            commit("setErrMsg", "")
            commit(DvLayout.Mutations.SET_FOCUS_STATUS, DvLayout.FocusStatus.FOCUSING_OUT)
            setTimeout(() => {
                commit(DvLayout.Mutations.SET_FOCUS_STATUS, DvLayout.FocusStatus.FOCUSED_OUT)
            }, getDuration())
        }
    },
    getters: {
        layoutClass: (state) => state.layoutStatus,
        focusClass: (state) => state.focusStatus,
        err: (state) => state.err
    }
}

export default DvLayoutModule
